package com.koron.system.company.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.system.company.mapper.CompanyMapper;
import com.koron.system.company.pojo.po.CompanyPO;
import com.koron.system.company.pojo.query.CompanyQuery;
import com.koron.system.company.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class CompanyServiceImpl implements CompanyService {


    @TaskAnnotation("page")
    @Override
    public Response page(SessionFactory sessionFactory, PageQuery pageQuery, CompanyQuery companyQuery) {
        CompanyMapper companyMapper = sessionFactory.getMapper(CompanyMapper.class);
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<CompanyPO> companyPOList = companyMapper.list(companyQuery);
        return Response.success(new PageInfo<>(companyPOList));
    }

    @TaskAnnotation("save")
    @Override
    public Response save(SessionFactory sessionFactory, CompanyPO companyPO) {
        CompanyMapper companyMapper = sessionFactory.getMapper(CompanyMapper.class);
        int result = companyMapper.insert(companyPO);
        return result > 0? Response.success(companyPO) : Response.fail();
    }

    @TaskAnnotation("delete")
    @Override
    public Response delete(SessionFactory sessionFactory, Integer id) {
        CompanyMapper companyMapper = sessionFactory.getMapper(CompanyMapper.class);
        Assert.notNull(id, "id cannot be null");
        int result = companyMapper.delete(id);
        return result > 0? Response.success() : Response.fail();
    }

    @TaskAnnotation("get")
    @Override
    public Response get(SessionFactory sessionFactory,CompanyQuery companyQuery) {
        CompanyMapper companyMapper = sessionFactory.getMapper(CompanyMapper.class);
        List<CompanyPO> companyPOList = companyMapper.list(companyQuery);
        return Response.success(companyPOList);
    }
}
